<script>

import { objectAssign, formatDate } from "../../utils/common";
import { server_request } from "../../utils/request";
import ServerUpload from "./ServerUpload";
import uploadConfig from "../../config/upload";
import * as qiniu from "qiniu-js";
import qiniuUpload from "../../utils/upload/qiniu";

/**
 *
    文件上传 - 七牛云
    使用前请先安装七牛组件
    安装命令：
    npm install qiniu-js
 * 
 */
export default {
    extends: ServerUpload,
    name: "PopoQiniuUpload",
    data(){
        return {
            sts: null,
            qiniu_token : null,
            qiniu_host: "",
        };
    },
    created(){
        // this.opt.action = uploadConfig.qiniu.action;
        // this.opt.data = this.form;
        this.opt.action = "";
        this.opt.httpRequest = this.httpRequest;
        this.sts = this.item.sts;
    },
    methods: {
        // getToken(){
        //     var promise = new Promise( ( resolve, reject) => {

        //         if( this.qiniu_token){
        //             resolve( this.qiniu_token);
        //         }else{
        //             try{
        //                 server_request( uploadConfig.qiniu.get_token_url).then( (res) => {
        //                     if( res.code == 200){
        //                         this.qiniu_token = res.data.token;
        //                         this.qiniu_host = res.data.host;
        //                         resolve( this.qiniu_token);
        //                     }else{
        //                         this.$message.error( "获取七牛凭证失败");
        //                         reject();
        //                     }
        //                 });
        //             }catch(e){
        //                 console.error( "获取七牛凭证失败", e);
        //                 this.$message.error( "获取七牛凭证失败");
        //                 reject();
        //             }
        //         }
        //     });
        //     return promise;
        // },
        /**
         * 上传OSS请求
         * 
         * @param  {[type]} http [description]
         * @return {[type]}      [description]
         */
        httpRequest( http){

            console.log( "before upload 111111" );
            qiniuUpload( 
                http.file, 
                {
                    sts: this.sts,
                    fileOnlinePath: this.getRandomFileInfo( http.file).final,
                    progress: (e)=>{
                        http.onProgress( { percent: e.percent});
                    },
                    success: (e)=>{
                        http.onSuccess(e)
                    },
                    failure: (e)=>{
                        console.error( "上传失败", e);
                        http.onError( "上传失败");
                    }
                }
            );

            return;

            // let file = http.file;
            // let qiniu_config = uploadConfig.qiniu;

            // let fileInfo = this.getRandomFileInfo( http.file);

            // var promise = this.getToken();
            // promise.then( ( token)=>{

            //     let final = fileInfo.final;
            //     if( final.substr(0,1) == "/"){
            //         final = final.substr(1);
            //     }
            //     let onlineFilePath = this.qiniu_host + fileInfo.final;
                
            //     // 开始上传
            //     var observable = qiniu.upload( http.file, final, token, {}, qiniu_config.upload_config);

            //     let subscription = observable.subscribe({
            //         next:( res)=>{
            //             // console.log( "subscribe next", res, onlineFilePath);
            //             http.onProgress( { percent: res.total.percent});
            //         },
            //         error:(err) => {
            //             console.error( "subscribe error", err, http);
            //             // 参数 err 为一个包含 code、message、isRequestError 三个属性的 object
            //             http.onError( err);
            //         },
            //         complete:( res) => {
            //             // console.log( "subscribe complete", res, onlineFilePath);
            //             http.onSuccess( {
            //                 code: 200,
            //                 msg: "success",
            //                 data: onlineFilePath
            //             });
            //         },
            //     });
            // });
        }
    }
};
</script>

